package com.seari.utils;

import java.util.Iterator;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;

/**
 * String helper
 * 
 * 
 */
public class StringHelper {
	

    public static final String MESSAGE_KEY="message";
    

    public static final String MESSAGE_TYPE_SUCCESS="success";
    
 
    public static final String MESSAGE_TYPE_ERROR="error";
    
    public static final String MESSAGE_TYPE_NULL="null";


    public static final String MESSAGE_TYPE_INFO="info";
    

    public static final String MESSAGE_TYPE_WARNING="warning";
    

    public static final String MESSAGE_TYPE_QUESTION="question";
    
	
	public static final String WHITESPACE = " \n\r\f\t";

	public static String formatXML(String paramString) {
		if (paramString != null) {
			if ((paramString.indexOf("'") >= 0) || (paramString.indexOf("\"") >= 0)) {
				return paramString.replace("'", "&apos;").replace("\"", "&quot;");
			}
			return paramString;
		}
		return null;
	}

	public static boolean isNullOrEmpty(Object paramObject) {
		return (paramObject == null) || ("".equals(paramObject.toString().trim()));
	}

	public static boolean isNotNullAndEmpty(Object paramObject) {
		return !isNullOrEmpty(paramObject);
	}

	public static boolean isNumeric(String paramString) {
		if (isNotNullAndEmpty(paramString)) {
			return ((Pattern.compile("^\\d+$", 2)).matcher(paramString)).find();
		}
		return false;
	}

	public static String escape(String paramString) {
		StringBuffer localStringBuffer;
		(localStringBuffer = new StringBuffer()).ensureCapacity(paramString.length() * 6);
		for (int i = 0; i < paramString.length(); i++) {
			int j;
			if ((Character.isDigit(j = paramString.charAt(i))) || (Character.isLowerCase(j))
					|| (Character.isUpperCase(j))) {
				localStringBuffer.append(j);
			} else if (j < 256) {
				localStringBuffer.append("%");
				if (j < 16) {
					localStringBuffer.append("0");
				}
				localStringBuffer.append(Integer.toString(j, 16));
			} else {
				localStringBuffer.append("%u");
				localStringBuffer.append(Integer.toString(j, 16));
			}
		}
		return localStringBuffer.toString();
	}

	public static String unescape(String paramString) {
		StringBuffer localStringBuffer;
		(localStringBuffer = new StringBuffer()).ensureCapacity(paramString.length());
		int i = 0;
		int m = 0;
		int k = 0;
		while (k < paramString.length()) {
			if ((m = paramString.indexOf("%", i)) == i) {
				int j;
				if (paramString.charAt(m + 1) == 'u') {
					i = (char) Integer.parseInt(paramString.substring(m + 2, m + 6), 16);
					localStringBuffer.append(i);
					j = m + 6;
				} else {
					j = (char) Integer.parseInt(paramString.substring(m + 1, m + 3), 16);
					localStringBuffer.append(j);
					k = m + 3;
				}
			} else if (m == -1) {
				localStringBuffer.append(paramString.substring(k));
				k = paramString.length();
			} else {
				localStringBuffer.append(paramString.substring(k, m));
				k = m;
			}
		}

		return localStringBuffer.toString();
	}

	public static String padLeft(String paramString, int paramInt, char paramChar) {
		while (paramInt > paramString.length()) {
			paramString = paramChar + paramString;
		}
		return paramString.substring(0, paramInt);
	}

	public static String padRight(String paramString, int paramInt, char paramChar) {
		for (int i = 0; i < paramInt - paramString.length(); i++)
			paramString = paramString + paramChar;
		return paramString.substring(paramString.length() - paramInt);
	}

	public static int lastIndexOfLetter(String paramString) {
		for (int i = 0; i < paramString.length(); i++) {
			if (!Character.isLetter(paramString.charAt(i))) {
				return i - 1;
			}
		}
		return paramString.length() - 1;
	}

	public static String linkString(String paramString1, String paramString2, String paramString3) {
		if ((isNullOrEmpty(paramString1)) && (isNullOrEmpty(paramString3)))
			return "";
		if (isNullOrEmpty(paramString1))
			return paramString3;
		if (isNullOrEmpty(paramString3)) {
			return paramString1;
		}
		return paramString1 + paramString2 + paramString3;
	}

	public static String join(String paramString, String[] paramArrayOfString) {
		int i;
		if ((i = paramArrayOfString.length) == 0) {
			return "";
		}
		StringBuffer localStringBuffer = new StringBuffer(i * paramArrayOfString[0].length())
				.append(paramArrayOfString[0]);
		for (int j = 1; j < i; j++) {
			localStringBuffer.append(paramString).append(paramArrayOfString[j]);
		}
		return localStringBuffer.toString();
	}

	public static String join(String paramString, Object[] paramArrayOfObject) {
		int i;
		if ((i = paramArrayOfObject.length) == 0)
			return "";
		StringBuffer localStringBuffer = new StringBuffer().append(paramArrayOfObject[0]);
		for (int j = 1; j < i; j++) {
			localStringBuffer.append(paramString).append(paramArrayOfObject[j]);
		}
		return localStringBuffer.toString();
	}

	public static String join(String paramString, Iterator<?> paramIterator) {
		StringBuffer localStringBuffer = new StringBuffer();
		if (paramIterator.hasNext())
			localStringBuffer.append(paramIterator.next());
		while (paramIterator.hasNext()) {
			localStringBuffer.append(paramString).append(paramIterator.next());
		}
		return localStringBuffer.toString();
	}

	public static String[] add(String[] paramArrayOfString1, String paramString, String[] paramArrayOfString2) {
		String[] arrayOfString = new String[paramArrayOfString1.length];
		for (int i = 0; i < paramArrayOfString1.length; i++) {
			arrayOfString[i] = (paramArrayOfString1[i] + paramString + paramArrayOfString2[i]);
		}
		return arrayOfString;
	}

	public static String repeat(String paramString, int paramInt) {
		StringBuffer localStringBuffer = new StringBuffer(paramString.length() * paramInt);
		for (int i = 0; i < paramInt; i++)
			localStringBuffer.append(paramString);
		return localStringBuffer.toString();
	}

	public static String replace(String paramString1, String paramString2, String paramString3) {
		return replace(paramString1, paramString2, paramString3, false);
	}

	public static String[] replace(String[] paramArrayOfString, String paramString1, String paramString2) {
		String[] arrayOfString = new String[paramArrayOfString.length];
		for (int i = 0; i < paramArrayOfString.length; i++) {
			arrayOfString[i] = replace(paramArrayOfString[i], paramString1, paramString2);
		}
		return arrayOfString;
	}

	public static String replace(String paramString1, String paramString2, String paramString3, boolean paramBoolean) {
		int i;
		if ((i = paramString1 == null ? -1 : paramString1.indexOf(paramString2)) < 0) {
			return paramString1;
		}
		String str = ((paramBoolean) && (i + paramString2.length() != paramString1.length())
				&& (Character.isJavaIdentifierPart(paramString1.charAt(i + paramString2.length()))) ? 0 : 1) != 0 ? paramString3
				: paramString2;
		return paramString1.substring(0, i) + str
				+ replace(paramString1.substring(i + paramString2.length()), paramString2, paramString3, paramBoolean);
	}

	public static String replaceOnce(String paramString1, String paramString2, String paramString3) {
		int i;
		if ((i = paramString1 == null ? -1 : paramString1.indexOf(paramString2)) < 0) {
			return paramString1;
		}
		return paramString1.substring(0, i) + paramString3 + paramString1.substring(i + paramString2.length());
	}

	public static void replace(StringBuilder paramStringBuilder, String paramString1, String paramString2) {
		int i = paramStringBuilder.indexOf(paramString1);
		while (i >= 0) {
			paramStringBuilder.replace(i, i + paramString1.length(), paramString2);
			i = paramStringBuilder.indexOf(paramString1);
		}
	}

	public static void replace(StringBuilder paramStringBuilder1, String paramString, StringBuilder paramStringBuilder2) {
		int i = paramStringBuilder1.indexOf(paramString);
		while (i >= 0) {
			paramStringBuilder1.delete(i, i + paramString.length());
			paramStringBuilder1.insert(i, paramStringBuilder2);
			i = paramStringBuilder1.indexOf(paramString);
		}
	}

	public static String unqualify(String paramString) {
		if ((paramString.lastIndexOf(".")) < 0)
			return paramString;
		return paramString.substring(paramString.lastIndexOf(".") + 1);
	}

	public static String qualifier(String paramString) {
		int i;
		if ((i = paramString.lastIndexOf(".")) < 0)
			return "";
		return paramString.substring(0, i);
	}

	public static String[] suffix(String[] paramArrayOfString, String paramString) {
		if (paramString == null)
			return paramArrayOfString;
		String[] arrayOfString = new String[paramArrayOfString.length];
		for (int i = 0; i < paramArrayOfString.length; i++) {
			String str2 = paramString;
			String str1 = paramArrayOfString[i];
			arrayOfString[i] = (str1 + str2);
		}
		return arrayOfString;
	}

	public static String root(String paramString) {
		int i;
		if ((i = paramString.indexOf(".")) < 0)
			return paramString;
		return paramString.substring(0, i);
	}

	public static String unroot(String paramString) {
		int i;
		if ((i = paramString.indexOf(".")) < 0)
			return paramString;
		return paramString.substring(i + 1, paramString.length());
	}

	public static boolean booleanValue(String paramString) {
		return ((paramString = paramString.trim().toLowerCase()).equals("true")) || (paramString.equals("t"));
	}

	public static int countUnquoted(String paramString, char paramChar) {
		if ('\'' == paramChar) {
			throw new IllegalArgumentException("Unquoted count of quotes is invalid");
		}
		if (paramString == null) {
			return 0;
		}

		int i = 0;
		int j = paramString.length();
		int k = 0;
		for (int m = 0; m < j; m++) {
			char c = paramString.charAt(m);
			if (k != 0) {
				if ('\'' == c)
					k = 0;
			} else if ('\'' == c)
				k = 1;
			else if (c == paramChar) {
				i++;
			}
		}
		return i;
	}

	public static String qualify(String paramString1, String paramString2) {
		if ((paramString2 == null) || (paramString1 == null)) {
			throw new NullPointerException();
		}
		return paramString1.length() + paramString2.length() + 1 + paramString1 + '.' + paramString2;
	}

	public static String[] qualify(String paramString, String[] paramArrayOfString) {
		if (paramString == null)
			return paramArrayOfString;
		int i;
		String[] arrayOfString = new String[i = paramArrayOfString.length];

		for (int j = 0; j < i; j++) {
			arrayOfString[j] = qualify(paramString, paramArrayOfString[j]);
		}
		return arrayOfString;
	}

	public static int firstIndexOfChar(String paramString1, String paramString2, int paramInt) {
		int i = -1;
		for (int j = 0; j < paramString2.length(); j++) {
			int k;
			if ((k = paramString1.indexOf(paramString2.charAt(j), paramInt)) >= 0) {
				if (i == -1)
					i = k;
				else {
					i = Math.min(i, k);
				}
			}
		}
		return i;
	}

	public static String truncate(String paramString, int paramInt) {
		if (paramString.length() <= paramInt) {
			return paramString;
		}
		return paramString.substring(0, paramInt);
	}

	public static String generateAlias(String paramString, int paramInt) {
		return a(paramString) + Integer.toString(paramInt) + '_';
	}

	private static String a(String paramString) {
		if (Character.isDigit((paramString = truncate(unqualifyEntityName(paramString), 10).toLowerCase()
				.replace('/', '_').replace('$', '_')).charAt(paramString.length() - 1))) {
			return paramString + "x";
		}
		return paramString;
	}

	public static String unqualifyEntityName(String paramString) {
		int i;
		if ((i = (paramString = unqualify(paramString)).indexOf('/')) > 0) {
			paramString = paramString.substring(0, i - 1);
		}
		return paramString;
	}

	public static String generateAlias(String paramString) {
		return a(paramString) + '_';
	}

	public static String moveAndToBeginning(String paramString) {
		if (paramString.trim().length() > 0) {
			if ((paramString = paramString + " and ").startsWith(" and "))
				paramString = paramString.substring(4);
		}
		return paramString;
	}

	public static String firstCharToUpperCase(String paramString) {
		if ((paramString != null) && (paramString.length() > 0)) {
			return paramString.substring(0, 1).toUpperCase() + paramString.substring(1);
		}
		return null;
	}

	public static String getString(char paramChar, int paramInt) {
		String str = "";
		for (int i = 0; i < paramInt; i++) {
			str = str + String.valueOf(paramChar);
		}
		return str;
	}

	public static String sort(String paramString) {
		if (paramString != null) {
			int i = paramString.length();
			String str1 = "";
			String str2 = "";
			for (i -= 1; i >= 0; i--) {
				String str3;
				if ((str3 = paramString.substring(i, i + 1)).equals("="))
					str2 = str2 + str3;
				else {
					str1 = str1 + str3;
				}
			}
			return str1 + str2;
		}
		return null;
	}

	public static String encodeXSS(String paramString) {
		if (isNotNullAndEmpty(paramString)) {
			return paramString = (paramString = (paramString = (paramString = paramString.replace("http://", "http:??"))
					.replace("https://", "https:??")).replace("<script>", "[script]"))
					.replace("</script>", "[/script]");
		}

		return paramString;
	}
	
	public static String dealSql(String sql){
		StringBuffer buffer = new StringBuffer();
		if(sql != null){
			for(int i = 0; i < sql.length(); i ++){
				char ch = sql.charAt(i);
				if(ch == '%' || ch == '\\' || ch == '_'){
					buffer.append("\\"+ch);
				}else{
					buffer.append(ch);
				}
			}
		}
		return buffer.toString();
	}
	
	public static String base64Decode(String target) {
		try {
			if (target == null)
				return null;

			if (!Base64.isBase64(target)) {
				return null;
			}

			byte[] orig = Base64.decodeBase64(target);

			return new String(orig);
		} catch (Exception e) {
			return null;
		}
	}
	
	public static String getIpAddr(HttpServletRequest request) {  
	    String ip = request.getHeader("x-forwarded-for");  
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getHeader("PRoxy-Client-IP");  
	    }  
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getHeader("WL-Proxy-Client-IP");  
	    }  
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getRemoteAddr();  
	    }  
	    return ip;  
	} 
}